﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AnimalFight.Lib;
using AnimalFight.Lib.Interfaces;
using AnimalFight.Lib.EventHandlers;

namespace AnimalFight.Lib.Data
{
	public class Dog : AnimalBase, IAttack, IDefend, IAttackable
	{
		public event DefendDelegate DefendEvent;
		public event AttackDelegate AttackEvent;
		private DogMood _mood;

		public void Attack(string targetName)
		{
			int theRealAttackPower = 0;

			if (this.Mood == DogMood.Bad)
			{
				theRealAttackPower = (int)(0.5 * this.AP);
			}
			else
			{
				theRealAttackPower = this.AP;
			}

			if (AttackEvent != null)
			{
				AttackEventHandler eventHandler = new AttackEventHandler
				{
                    AnimalName = targetName,
					AP = theRealAttackPower
				};

				AttackEvent(this, eventHandler);
			}
		}


		public DogMood Mood
		{
			get { return _mood; }
			set { _mood = value; }
		}
		

		public bool IsDefending
		{
			get;
			set;
		}

		public void Defend()
		{
			if (DefendEvent != null)
			{
				DefendEvent(this, new DefendEventHandler 
				{
					AnimalName = this.Name, 
					Target = Animals.Dog 
				});
			}
		}

		public void Attacked(int ap)
		{
			if (this._hasEscaped)
			{
				Console.WriteLine("{0} has escaped. Cannot be attacked.", this.Name);
			}
			else if (this._isDead)
			{
				Console.WriteLine("{0} has already dead. Cannot be attacked.", this.Name);
			}
			else
			{
                Console.WriteLine("{0} has been attacked. Current HP = {1}", this.Name, this.HP);

				int realDP = this.IsDefending ? this.DP + 10 : this.DP;
				realDP = this.Mood == DogMood.Bad ? realDP - 10 : realDP;

				this.HP -= (ap - realDP);

                Console.WriteLine("HP is decreased -{1}", this.Name, ap - realDP);
                Console.WriteLine("Current HP = {0}", this.HP);

                if (this.HP <= 0)
                {
                    this.IsDead = true;
                }
			}
		}
	}

	public enum DogMood { Good, Bad }
}
